﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace hCommand {
    public class ComforData {

        public String id { get; set; }
        public String create_time { get; set; }
        public String resource_type { get; set; }
        public String event_type { get; set; }
        public String summary { get; set; }
        public Resource resource { get; set; }
    }



    public class Resource {
        public String original_type { get; set; }
        public String algorithm { get; set; }
        public String ciphertext { get; set; }
        public String associated_data { get; set; }
        public String nonce { get; set; }

        public String getDate() {
            return AesGcmDecryptFromBase64("sgj123dyyhhhhjfasdf097dskajskjnk", nonce, ciphertext, associated_data);
        }

        string AesGcmDecryptFromBase64(string key, string nonce, string encryptedData, string associatedData) {
            var keyBytes = Encoding.UTF8.GetBytes(key);
            var nonceBytes = Encoding.UTF8.GetBytes(nonce);
            var associatedBytes = associatedData == null ? null : Encoding.UTF8.GetBytes(associatedData);

            var encryptedBytes = Convert.FromBase64String(encryptedData);
            //tag size is 16
            var cipherBytes = encryptedBytes[..^16];
            var tag = encryptedBytes[^16..];
            var decryptedData = new byte[cipherBytes.Length];
            using var cipher = new AesGcm(keyBytes);
            cipher.Decrypt(nonceBytes, cipherBytes, tag, decryptedData, associatedBytes);
            return Encoding.UTF8.GetString(decryptedData);
        }
    }




}
